package com.xinchuang.util;

import javax.servlet.http.HttpServletRequest;

/**
 * TODO 获取请求的Ip地址
 *
 * @author zkwczx
 * @version 1.0
 * @date 2021/3/19 17:08
 */
public class IPUtils {

    public static void main(String[] args) {
        String str = "C:/Users/cathy/Desktop/交通系统后端/local/865346268950564864.db";
        System.out.println(str.lastIndexOf("local"));
        System.out.println(str.substring(str.lastIndexOf("local"), str.length()));
    }

    private static final String X_FORWARDED_FOR = "x-forwarded-for";

    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader(X_FORWARDED_FOR);
        if (ip == null||ip.length()==0||"unknown".equalsIgnoreCase(ip)){
            ip = request.getHeader("Proxy-client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;

    }

}
